package net.oschina.simpleplus.gen;

import org.apache.commons.lang3.StringUtils;

public class ProtoField {

	protected String name;
	
	protected String javaType;

	public ProtoField() {
	}

	public ProtoField(String name, String javaType) {
		super();
		this.name = name;
		this.javaType = parse(javaType);
		
		System.out.println(name+","+javaType);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getJavaType() {
		return javaType;
	}

	public void setJavaType(String javaType) {
		this.javaType = javaType;
	}
	
	
	public StringBuffer appendField(StringBuffer buffer){
		return buffer.append("\n\t")
			.append("private ")
			.append(javaType)
			.append(" ")
			.append(name)
			.append(";");
	}

	public StringBuffer appendGetterMethod(StringBuffer buffer){
		if("boolean".equals(javaType)){
			return buffer.append("\n\t")
					.append("public ")
					.append(javaType)
					.append(" is")
					.append(StringUtils.capitalize(name))
					.append("(){\n\t\t")
					.append("return this.")
					.append(name)
					.append(";\n\t}");
		}
		return buffer.append("\n\t")
		.append("public ")
		.append(javaType)
		.append(" get")
		.append(StringUtils.capitalize(name))
		.append("(){\n\t\t")
		.append("return this.")
		.append(name)
		.append(";\n\t}");
	}

	public StringBuffer appendSetterMethod(StringBuffer buffer){
		return buffer.append("\n\t")
		.append("public ")
		.append("void ")
		.append("set")
		.append(StringUtils.capitalize(name))
		.append("(")
		.append(javaType)
		.append(" ")
		.append(name)
		.append("){\n\t\t")
		.append("this.")
		.append(name)
		.append(" = ")
		.append(name)
		.append(";\n\t}");
	}

	public StringBuffer appendBuildMethod(StringBuffer buffer){
		if("byte[]".equals(javaType)){
			return buffer.append("\n\t\t")
					.append("builder.set")
					.append(StringUtils.capitalize(name))
					.append("(ByteString.copyFrom(")
					.append(name)
					.append("));"); 
		}
		return buffer.append("\n\t\t")
		.append("builder.set")
		.append(StringUtils.capitalize(name))
		.append("(")
		.append(name)
		.append(");");
	}

	public StringBuffer appendCopyFromMethod(StringBuffer buffer){
		if("byte[]".equals(javaType)){
			return buffer.append("\n\t\t")
					.append("this.")
					.append(name)
					.append(" = t.get")
					.append(StringUtils.capitalize(name))
					.append("().toByteArray();");
		}
		return buffer.append("\n\t\t")
				.append("this.")
				.append(name)
				.append(" = t.get")
				.append(StringUtils.capitalize(name))
				.append("();");
	}
	
	public StringBuffer appendEmptyConstructorMethod(StringBuffer buffer){
		return buffer.append("\n\t\tthis.")
					 .append(name)
					 .append(" = ")
					 .append(defaultInitlized())
					 .append(";");
	}
	
	protected String defaultInitlized() {
		if("int".equals(javaType) 
				|| "float".equals(javaType)
				|| "long".equals(javaType)
				|| "double".equals(javaType))
			return "0";
		if("boolean".equals(javaType)){
			return "false";
		}
		
		if("String".equals(javaType)){
			return "\"\"";
		}
		
		if("byte[]".equals(javaType)){
			return "\"\".getBytes()";
		}
		return null;
		
	}
	
	private static String parse(String str) {
		return str.substring(str.lastIndexOf(".") + 1, str.length());
	}
}
